package com.life.ai.server.common.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.life.ai.server.common.utils.ValidatorUtil;

/**
 * CostomExcelListener
 *
 * @author kylewka
 * @since 2023/9/1
 */
public class CustomExcelListener<T> extends DefaultExcelListener<T> {

    /**
     * 返回成功导入的数据
     */
    protected Boolean returnData = Boolean.FALSE;

    private final ExcelConsumer<T> consumer;

    public CustomExcelListener(ExcelConsumer<T> consumer, boolean isValidate, boolean breakOnError,boolean returnData) {
        super(isValidate, breakOnError);
        this.consumer = consumer;
        this.returnData = returnData;
    }

    @Override
    public void invoke(T data, AnalysisContext analysisContext) {
        if (isValidate) {
            ValidatorUtil.validate(data);
        }
        consumer.accept(data);  // 执行自定义处理方法
        if (returnData){
            excelResult.getDataList().add(data);
        }
        excelResult.increment(); // 成功数量计数+1
    }
}
